package org.timemachine.listener;

import javax.naming.NamingException;
import javax.persistence.PostRemove;

import org.timemachine.action.facade.EntityChangeFacade;
import org.timemachine.factory.EntityChangeFactory;
import org.timemachine.factory.spi.EntityChange;

public class TimeMachineRemoveListener extends TimeMachineBaseListener {

	@PostRemove
	public void postRemove(Object entity) throws Exception {
		try {

			if (isEntityHasChangeAnno(entity)) {
				EntityChange entityChange = getEntityChangeForEntity(entity);		
				
				EntityChangeFactory.copyProperties(entityChange, entity);
				// TODO @ID 'den primary key ve değeri bulunacak , reflection ile EntityChange'ler oluşturulurken
				//ClassHelperImpl classHelperImpl = new ClassHelperImpl();
				//Object primaryKeyValue = classHelperImpl.findFieldValue(entity, entity.getClass().getSuperclass(), entityChange.getEntityPrimaryKeyName());
				//EntityChangeFactory.copyProperty(entityChange, entityChange.getEntityPrimaryKeyName(), primaryKeyValue);
				entityChange.setChangeType(EntityChangeFactory.CHANGE_TYPE_REMOVE);
	
				EntityChangeFacade entityChangeFacade = findEntityChangeFacade();
				entityChangeFacade.removeEntityChange(entityChange);
			}
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

}
